//
//  QDTabBarController.m
//  simulateQuickDoctor
//
//  Created by 梁家伟 on 17/3/22.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDTabBarController.h"
#import "QDNavigationController.h"
#import "CZAdditions.h"
#import "QDSliderController.h"
#import "GDGuideView.h"
#import "QDBaseController.h"
#import "QDUserDefaultTool.h"
//图片的个数
#define IMAGECOUNT 4
@interface QDTabBarController ()

@end

@implementation QDTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBar.tintColor = [UIColor cz_colorWithRed:0 green:210 blue:175];
    self.tabBar.backgroundColor = [UIColor whiteColor];
    [self addChildControllers];
    
    
    //检查当前的版本号是否和旧的的版本号一致 不一样添加开场页面
    NSString *versionKey = @"CFBundleVersion";
    NSString *footVersionKey = [QDUserDefaultTool objetForKey:versionKey];
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[versionKey];
    if (![footVersionKey isEqualToString:currentVersion]) {
        [self  makeGuideView];
        //更新本地版本号
        [QDUserDefaultTool saveObject:currentVersion forKey:versionKey];
    }
    //将旧的版本号设置为新的版本号存储
    footVersionKey = currentVersion;
    
//    //创建新特性界面
//    [self makeGuideView];
}


-   (void)addChildControllers{
    
    
    NSArray* channelArr = @[
                            @{
                                @"className":@"QDHomeController",
                                @"title":@"首页",
                                @"icon":@"1"
                                },
                            
                            @{
                                @"className":@"QDFoundController",
                                @"title":@"发现",
                                @"icon":@"2"
                                },
                            
                            @{
                                @"className":@"QDHealthController",
                                @"title":@"健康",
                                @"icon":@"3"
                                },
                            
                            ];
    
    
    for (NSDictionary* dict in channelArr) {
        [self addChildViewController:[self setTabVCWithDictionary:dict]];
    }
}


- (UINavigationController*)setTabVCWithDictionary:(NSDictionary*)dict{
    
    UIViewController* vc;

    if ([dict[@"title"] isEqualToString:@"首页"]) {
        
        UIStoryboard *board = [UIStoryboard storyboardWithName:@"QDHomeVC" bundle:nil];
        vc = [board instantiateInitialViewController];
        
    } else {
        
        vc = [[NSClassFromString(dict[@"className"]) alloc]init];
        
       
        
    }
    
    vc.title = dict[@"title"];
    vc.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    QDNavigationController* nav = [[QDNavigationController alloc]initWithRootViewController:vc];
    
    vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"nav_menu_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(slideToRight)];
    
    nav.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"tab_normal_%@",dict[@"icon"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"tab_normal_%@_selected",dict[@"icon"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    return nav;
}


- (void)slideToRight{
    
    QDSliderController* parentVC = (QDSliderController*)[self parentViewController];
    [parentVC slideToRight];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) makeGuideView {
    
    // 1.创建新特性界面
    GDGuideView *guideView = [[GDGuideView alloc] initWithFrame:self.view.bounds];
    // 2.给新特性界面传递数据
    guideView.imageNames = [self loadImageNames];
    // 3.添加新特性界面到控制器view上
    [self.view addSubview:guideView];
}



- (void)pushControllerWithStoryName:(NSString*)sbName andIdentifier:(NSString*)identifier andPara:(NSDictionary*)params{
    UIStoryboard  *sb = [UIStoryboard storyboardWithName:sbName bundle:nil];

    QDBaseController* vc = [sb instantiateViewControllerWithIdentifier:identifier];
        
    QDNavigationController* nav = (QDNavigationController*)self.selectedViewController;
    
    vc.parameters = params;
    
    [nav pushViewController:vc animated:YES];
    
}



- (void)pushControllerWithName:(NSString*)vcName andPara:(NSDictionary*)params{
    
    QDBaseController* vc = [[NSClassFromString(vcName) alloc]init];
    vc.view.backgroundColor = [UIColor whiteColor];
    QDNavigationController* nav = (QDNavigationController*)self.selectedViewController;
    
    vc.parameters = params;
    
    [nav pushViewController:vc animated:YES];
    
}


#pragma mark - 加载数据
- (NSArray *)loadImageNames {
    
    // 1.创建可变数据用来保存图片名称 // Capacity 容量
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:IMAGECOUNT];
    
    // 2.for循环拼接图片名称
    for (NSInteger i = 0; i < IMAGECOUNT; i++) {
        // 2.1 拼接图片名称
        NSString *imageName = [NSString stringWithFormat:@"Guide_%zd", i + 1];
        
        //        UILabel *titleLabel = [[UILabel alloc]init];
        //        _TitleLabel = titleLabel;
        
        
        // 2.2 添加图片名称到可变数据中
        [arrM addObject:imageName];
    }
    // 可变数组进行不可变拷贝,变了不可变
    return arrM.copy;
}

@end
